<?php

class CompareController extends GxController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('CompareclassAddClassById', 'RemovecompareclasscourseById', 'view'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array(''),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionCompareclassAddClassById() {
        $checkIds = '';
        
        if (Yii::app()->session['allcompare_ids'] <> "") {
            $compare_class_course_id = Yii::app()->session['allcompare_ids'];
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
            if (in_array($_POST['class_course_id'], $checkIds) || count($checkIds) >= 4) {
                return false;
            }
        } else {
            $compare_class_course_id = '';
            Yii::app()->session['classcoursetype'] = '';
        }



        if ($_POST['classcoursetype'] == 'class') {
            $compare_class_course_id.= $_POST['class_course_id']."@".$_POST['classcoursetype'] . ',';
            Yii::app()->session['allcompare_ids'] = $compare_class_course_id;
            $classcourseDetail = Classes::model()->findByPk($_POST['class_course_id']);
            $class_course_titile = $classcourseDetail->class_titile;
			$imagePath = getImagePathByClassCourse('Classes', $_POST['class_course_id']);
			$imageName = (ClassImage::getPrimaryImage($_POST['class_course_id']) != '') ? baseUrl() . '../../' . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($_POST['class_course_id']) : baseUrl() . '../../' . getUploadDir() . '/no-class-image.jpg';
        } else {
            $compare_class_course_id.= $_POST['class_course_id']."@".$_POST['classcoursetype'] . ',';
            Yii::app()->session['allcompare_ids'] = $compare_class_course_id;
            $classcourseDetail = Courses::model()->findByPk($_POST['class_course_id']);
            $class_course_titile = $classcourseDetail->course_titile;
            $imagePath = getImagePathByClassCourse('Courses', $_POST['class_course_id']);
			$imageName = (ClassImage::getPrimaryImage($_POST['class_course_id']) != '') ? baseUrl() . '../../' . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($_POST['class_course_id']) : baseUrl() . '../../' . getUploadDir() . '/no-class-image.jpg';
        }

       $classcourseImg = '<img src="' . $imageName . '" height="70" width="70"  alt="' . $class_course_titile . '">';

        if (Yii::app()->session['allcompare_ids'] <> "") {
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
        }

        echo $return_data = $class_course_titile . '#@#' . $classcourseImg . '#@#' . count($checkIds);
    }

    public function actionRemovecompareclasscourseById() {

        if (Yii::app()->session['allcompare_ids'] <> "") {
            $compare_class_course_id = Yii::app()->session['allcompare_ids'];
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
			$key = array_search($_POST['class_course_id'], $checkIds);
            if (false !== $key) {
				unset($checkIds[$key]);
            }
        }
        
        $OutPutdata = '';
        $remainingIds = '';

      
		if (count($checkIds) > 0) {
            for ($i = 0; $i <= count($checkIds); $i++) {

                if (isset($checkIds[$i]) && $checkIds[$i] <> "") {
                    $remainingIds.=$checkIds[$i] . ',';
					$getcheckIds  = explode("@",$checkIds[$i]); 

					if (isset($getcheckIds[0]) && $getcheckIds[1] == "class") {
                        /* This is for class */
                        $classcourseDetail = Classes::model()->findByPk($getcheckIds[0]);
                        $class_course_titile = $classcourseDetail->class_titile;
						$imagePath = getImagePathByClassCourse('Classes', $getcheckIds[0]);
						$imageName = (ClassImage::getPrimaryImage($getcheckIds[0]) != '') ? baseUrl() . '../../' . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($getcheckIds[0]) : baseUrl() . '../../' . getUploadDir() . '/no-class-image.jpg';
                    } elseif(isset($getcheckIds[0]) && $getcheckIds[1] == "course") {
                        /* This is for course */
                        $classcourseDetail = Courses::model()->findByPk($getcheckIds[0]);
                        $class_course_titile = $classcourseDetail->course_titile;
						$imagePath = getImagePathByClassCourse('Courses', $getcheckIds[0]);
						$imageName = (ClassImage::getPrimaryImage($getcheckIds[0]) != '') ? baseUrl() . '../../' . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($getcheckIds[0]) : baseUrl() . '../../' . getUploadDir() . '/no-class-image.jpg';
                    }
						$classcourseImg = '<img src="' . $imageName . '" height="70" width="70"  alt="' . $class_course_titile . '">';

                    $OutPutdata.='<li id="' . $getcheckIds[0] . '"><a href="javascript:void(0);" onclick="RemoveCompareList(' . $classcourseDetail->id . ',\'' . $getcheckIds[1] . '\');" class="sprite cross">&nbsp;</a>' . $classcourseImg . '<p class="product">' . $class_course_titile . '</p><input type="hidden" class="compareids" name="class_course_id[]" value="' . $getcheckIds[0] . '" /></li>';
                }
            }
        } else {
            $session = new CHttpSession;
			unset($_SESSION['allcompare_ids']);
			unset($_SESSION['classcoursetype']);
			$OutPutdata = 'no';
        }
        Yii::app()->session['allcompare_ids'] = $remainingIds;

        echo $OutPutdata;
    }

    public function actionView() {
         $this->layout = 'detailspage';
        if (!empty($_POST['class_course_id'])) {
            
			$session = new CHttpSession;
			unset($_SESSION['allcompare_ids']);
			unset($_SESSION['classcoursetype']);
			
			$_POST['class_course_id'] = $_POST['class_course_id'];
            $this->render('view', array('allcompareClasses' => $_POST['class_course_id']));
			
        } else {
            throw new CHttpException(400, Yii::t('app', Message::Invalid_Request));
        }
    }

}
